﻿using Forum.BaseController;
using Forum.Model.QueryFilters;
using Forum.Service.Interface;

namespace Forum.Controllers
{
    /// <summary>
    /// 系统信息
    /// </summary>
    [Route("Api/Basic")]
    public class BasicInfoController : BaseController<IBasicService>
    {

        public BasicInfoController(IBasicService defaultService, ILogger<BaseController<IBasicService>> logger) : base(defaultService, logger)
        {

        }
        #region 网站信息

        /// <summary>
        /// 网站信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("Site")]
        public Site GetSiteInfo()
        {
            return DefaultService.GetSiteInfo();
        }

        /// <summary>
        /// 更新网站信息
        /// </summary>
        /// <returns></returns>
        [HttpPut("Site")]
        public async Task<ApiResponse> UpdateSiteInfo(Site site)
        {
            await DefaultService.UpdateSiteInfo(site);
            return ApiResponse.OK();
        }

        #endregion

        #region 社交信息

        /// <summary>
        /// 批量查询社交信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("SocialList")]
        public List<Social> GetSocialList()
        {
            return DefaultService.GetSocialList();
        }

        #endregion

        #region 友链

        /// <summary>
        /// 查询友情链接
        /// </summary>
        /// <returns></returns>
        [HttpGet("LinkList")]
        public List<Link> GetLinkList()
        {
            return DefaultService.GetLinkList();
        }

        /// <summary>
        /// 增加友情链接
        /// </summary>
        /// <returns></returns>
        [HttpPost("AddLink")]
        public ApiResponse AddLink(LinkReq req)
        {
            return DefaultService.AddLink(req);
        }

        /// <summary>
        /// 查询友链（分页）
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpGet("GetLinkList")]
        public ApiResponsePaged<Link> GetList([FromQuery] QueryParameters param)
        {
            var pagedList = DefaultService.GetLinkPagedList(param);
            return new ApiResponsePaged<Link>
            {
                data = pagedList.ToList(),
                total = pagedList.Total,
                haveMore = pagedList.Total > param.Page * param.PageSize
            };
        }

        /// <summary>
        /// 加入友链
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("{id}/[action]")]
        public bool JoinLink(string id)
        {
            return DefaultService.UpdateLink(id, true);
        }
        /// <summary>
        /// 取消友链
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("{id}/[action]")]
        public bool CancelLink(string id)
        {
            return DefaultService.UpdateLink(id, false);
        }

        /// <summary>
        /// 删除友链
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}/[action]")]
        public bool DeleteLink(string id)
        {
            return DefaultService.DeleteLink(id);
        }

        #endregion

        #region 系统参数

        /// <summary>
        /// 查询系统参数（分页）
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpGet("Params")]
        public ApiResponsePaged<SysParameters> GetParams([FromQuery] QueryParameters param)
        {
            var pagedList = DefaultService.GetParamPagedList(param);
            return new ApiResponsePaged<SysParameters>
            {
                data = pagedList.ToList(),
                total = pagedList.Total,
                haveMore = pagedList.Total > param.Page * param.PageSize
            };
        }

        /// <summary>
        /// 删除系统参数
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}/Params")]
        public ApiResponse DeleteParam(string id)
        {
            var post = DefaultService.GetByParamId(id);
            if (post == null) return ApiResponse.NotFound($"系统参数不存在");
            DefaultService.DeleteParam(id);
            return ApiResponse.OK();
        }

        /// <summary>
        /// 添加系统参数
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost("Params")]
        public ApiResponse AddParam(ParamsReq req)
        {
            DefaultService.AddParam(req);
            return ApiResponse.OK();
        }

        /// <summary>
        /// 更新系统参数
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPut("Params")]
        public ApiResponse UpdateParam(SysParameters req)
        {
            var post = DefaultService.GetByParamId(req.Id);
            if (post == null) return ApiResponse.NotFound($"系统参数不存在");
            DefaultService.UpdateParam(req);
            return ApiResponse.OK();
        }

        #endregion

    }
}
